﻿using System.Web.Mvc;
using System.IO.Compression;
using System.Web.Optimization;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace SelfManagement.Mvc.Bundle
{
    /// <summary>
    /// 转换样式中的路径
    /// </summary>
    public class ResolveUrlStyleBundle : System.Web.Optimization.Bundle
    {
        public ResolveUrlStyleBundle(string virtualPath) : base(virtualPath, new IBundleTransform[]
        {
            new CssMinify()
        })
        {
        }

        public ResolveUrlStyleBundle(string virtualPath, string cdnPath) : base(virtualPath, cdnPath, new IBundleTransform[]
        {
            new CssMinify()
        })
        {
        }

        public override System.Web.Optimization.Bundle Include(params string[] virtualPaths)
        {
            foreach (var virtualPath in virtualPaths)
            {
                base.Include(virtualPath, new CssRewriteUrlTransform());
            }
            return this;
        }

        public override System.Web.Optimization.Bundle Include(string virtualPath, params IItemTransform[] transforms)
        {
            if (transforms.Count(t => t.GetType().Name == "CssRewriteUrlTransform") == 0)
            {
                var transformsLst = transforms.ToList();
                transformsLst.Add(new CssRewriteUrlTransform());
                return base.Include(virtualPath, transformsLst.ToArray());
            }
            else
            {
                return base.Include(virtualPath, transforms);
            }
        }
    }
}